//
//  CAScrollLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/17.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CAScrollLayerViewController.h"
#import "CAScrollerLayerImageView.h"

@interface CAScrollLayerViewController ()

@property (nonatomic, strong) CAScrollerLayerImageView *caScrollerLayerImageV;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) CAScrollLayer *scrollerLayer;

@end

@implementation CAScrollLayerViewController

/**
 *  [self.scrollerLayer scrollToPoint:orgin]; // 滑动到某处
     [self.scrollerLayer scrollRectToVisible:CGRectMake(160, 274, 160, 274)]; // 和scrollToRect一样
     [self.scrollerLayer scrollToRect:CGRectMake(160, 274, 160, 274)]; // 滑动到某区域
     [self.scrollerLayer scrollPoint:orgin]; //和scrollToPoint方法一样
 
 scrollerMode:CAScrollLayer支持的滚动方向
 CA_EXTERN NSString * const kCAScrollNone      不能滚动
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 CA_EXTERN NSString * const kCAScrollVertically       垂直滚动
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 CA_EXTERN NSString * const kCAScrollHorizontally     水平滚动
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
 CA_EXTERN NSString * const kCAScrollBoth         水平和垂直都可以，默认为此值
 __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
     [self.view addSubview:_backgroundView];
#pragma mark - 写一个UIImageView的子类试验
   
//    
//    self.caScrollerLayerImageV = [[CAScrollerLayerImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
//    [self.view addSubview:_caScrollerLayerImageV];
//    
//    CALayer *imageLayer = [CALayer layer];
//        imageLayer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"snowMan"].CGImage);
//        imageLayer.frame = CGRectMake(0, 0, 320, 568);
//    [self.caScrollerLayerImageV.layer addSublayer:imageLayer];
   
#pragma mark - 直接在控制器中试验
    CALayer *imageLayer = [CALayer layer];
    imageLayer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"snowMan"].CGImage);
    imageLayer.frame = CGRectMake(0, 0, 320, 568);
    
    self.scrollerLayer = [CAScrollLayer layer];
    self.scrollerLayer.frame = CGRectMake(60, 60, 200, 200);
    [self.scrollerLayer addSublayer:imageLayer];
    self.scrollerLayer.scrollMode = kCAScrollBoth;
    self.scrollerLayer.shadowColor = [UIColor redColor].CGColor;
    self.scrollerLayer.shadowOpacity = 1;
    self.scrollerLayer.shadowOffset = CGSizeMake(0, 0);
    self.scrollerLayer.scrollMode =  kCAScrollBoth; // CAScrollLayer的支持的滑动方向
    [self.backgroundView.layer addSublayer:_scrollerLayer];
    
    [self.scrollerLayer scrollToRect:CGRectMake(160, 274, 160, 274)]; // 滑动到某区域
  //  [self.scrollerLayer scrollRectToVisible:CGRectMake(160, 274, 160, 274)]; // 和scrollToRect一样
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];
    [self.backgroundView addGestureRecognizer:panGesture];
//
    
}

- (void)gesture:(UIPanGestureRecognizer *)gesture
{
    CGPoint translation = [gesture translationInView:self.view];
    CGPoint orgin = self.scrollerLayer.bounds.origin;
    orgin = CGPointMake(orgin.x - translation.x, orgin.y - translation.y);
    [self.scrollerLayer scrollToPoint:orgin];
 //   [self.scrollerLayer scrollPoint:orgin]; //和scrollToPoint方法一样
    [gesture setTranslation:CGPointZero inView:self.view];
}


@end
